/*
 开发人员 : tss
 创建时间 : 2020/6/21 9:36 下午
 文件名称 : main.go
 开发工具 : GoLand
*/

package main

import (
	"fmt"
	"image/color"
	"gocv.io/x/gocv"
)

func main() {
	deviceID := 0

	webcam, err := gocv.VideoCaptureDevice(int(deviceID))
	if err != nil {
		fmt.Println(err)
		return
	}
	defer webcam.Close()

	window := gocv.NewWindow("人脸识别")
	window.ResizeWindow(960,540)
	defer window.Close()

	img := gocv.NewMat()
	defer img.Close()

	blue := color.RGBA{	255,255,0, 0}

	classifier := gocv.NewCascadeClassifier()
	defer classifier.Close()

	if !classifier.Load("haarcascade_frontalface_default.xml") {
		fmt.Println("Error")
		return
	}

	fmt.Printf("start reading camera device: %v\n", deviceID)
	for {
		if ok := webcam.Read(&img); !ok {
			fmt.Printf("cannot read device %d\n", deviceID)
			return
		}
		if img.Empty() {
			continue
		}

		rects := classifier.DetectMultiScale(img)
		fmt.Printf("found %d faces\n", len(rects))

		for _, r := range rects {
			gocv.Rectangle(&img, r, blue, 3)
		}

		window.IMShow(img)
		window.WaitKey(1)
	}
}